package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.*;
import pers.cj.pdos.service.*;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.RetailSetVo;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/sett")
public class RetailSetController extends BaseController {
    @Autowired
    private IRetailSetService retailSetService;
    @Autowired
    private IUserHangService userHangService;

    @RequiresPermissions("retailSet:query")
    @RequestMapping("/retailSet")
    public String retailSet() {
        return "sett/retailSet/retailSet";
    }

    @RequiresPermissions("retailSet:insert")
    @RequestMapping("/retailSetNew")
    public String retailSetNew() {
        return "sett/retailSet/retailSetNew";
    }

    @RequiresPermissions("retailSet:insert")
    @RequestMapping("/retailSetEdit")
    public String retailSetEdit(Model model,String id) {
        model.addAttribute("id",id);
        return "sett/retailSet/retailSetEdit";
    }

    @ResponseBody
    @RequiresPermissions("retailSet:query")
    @RequestMapping("/retailSet/getList")
    public ResponseList getList(int page, int limit){
        Company company = getCompany();
        List<RetailSetVo> list = retailSetService.listVo(page,limit,company.getId());
        long count = retailSetService.count(company.getId());
        return ResponseList.success(count,list);
    }

    @ResponseBody
    @RequiresPermissions("retailSet:insert")
    @RequestMapping("/retailSet/add")
    public ResponseEntity add(@RequestBody RetailSet retailSet){
        Company company = getCompany();
        if (Utils.isEmpty(retailSet.getStoreId())) {
            return ResponseEntity.fail("门店不能为空");
        }

        if (Utils.isEmpty(retailSet.getHouseId())) {
            return ResponseEntity.fail("仓库不能为空");
        }
        if (Utils.isEmpty(retailSet.getCashSettAccountId())) {
            return ResponseEntity.fail("现金账户不能为空");
        }

        if (Utils.isEmpty(retailSet.getCustomId())) {
            return ResponseEntity.fail("默认客户不能为空");
        }

        if (retailSetService.checkStore(company.getId(),retailSet.getStoreId())){
            return ResponseEntity.fail("门店已有对应的零售收银设置，请更换门店");
        }

        DataUtil.processEntity(getCompany(), retailSet);
        if (Utils.isEmpty(retailSet.getCardSettAccountId())){
            retailSet.setCardSettAccountId(null);
        }
        retailSetService.save(retailSet);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("retailSet:insert")
    @RequestMapping("/retailSet/update")
    public ResponseEntity update(@RequestBody RetailSet retailSet){
        Company company = getCompany();
        if (Utils.isEmpty(retailSet.getStoreId())) {
            return ResponseEntity.fail("门店不能为空");
        }

        if (Utils.isEmpty(retailSet.getHouseId())) {
            return ResponseEntity.fail("仓库不能为空");
        }
        if (Utils.isEmpty(retailSet.getCashSettAccountId())) {
            return ResponseEntity.fail("现金账户不能为空");
        }
        if (Utils.isEmpty(retailSet.getCustomId())) {
            return ResponseEntity.fail("默认客户不能为空");
        }

        RetailSet old = retailSetService.getById(retailSet.getId());
        if (old == null) {
            return ResponseEntity.fail("未找到对应的零售设置");
        }

        if (!old.getStoreId().equals(retailSet.getStoreId()) && retailSetService.checkStore(company.getId(),retailSet.getStoreId())){
            return ResponseEntity.fail("门店已有对应的零售收银设置，请更换门店");
        }


        DataUtil.processEntity(false, getCompany(), retailSet);
        if (Utils.isEmpty(retailSet.getCardSettAccountId())){
            retailSet.setCardSettAccountId(null);
        }
        retailSetService.update(retailSet);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("retailSet:delete")
    @RequestMapping("/retailSet/delete")
    public ResponseEntity delete(@RequestBody RetailSet retailSet){
        retailSetService.remove(retailSet);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequestMapping("/retailSet/getById")
    public ResponseData getById(String id){
        if (Utils.isEmpty(id)) {
            return ResponseData.fail("参数id不能为空");
        }

        RetailSet retailSet = retailSetService.getById(id);
        if (retailSet == null){
            return ResponseData.fail("参数id不正确");
        }

        return ResponseData.success(retailSet);
    }
}
